package controllers

import (
	"WebSvr/modules/common"
	"WebSvr/modules/mysql"
	"net/http"
	"strconv"
	"text/template"

	"github.com/astaxie/beego"
)

type WebServerCfgController struct {
	beego.Controller
}

func (this *WebServerCfgController) GetAdminHome() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			{
				this.Data["xsrf_token"] = this.XSRFToken()
				this.Data["adminName"] = userName
				RecordUserAction(userName, accessInt, ip, "获取adminHome页")
				this.TplName = "adminhome.html"
			}
		case common.ACCESS_REPORT:
			fallthrough
		default:
			{
				RecordUserAction(userName, accessInt, ip, "获取adminHome页失败转404")
				this.TplName = "404.html"
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

// "/admingetServerData"
func (this *WebServerCfgController) GetAllServerDataByAdmin() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			{
				webAdmin := mysql.GetWebAdmin()
				jsonSlice := webAdmin.GetAllServerCfg()
				RecordUserAction(userName, accessInt, ip, "admingetServerData")
				this.Data["json"] = jsonSlice
				this.ServeJSON()
			}
		case common.ACCESS_REPORT:
			fallthrough
		default:
			{
				RecordUserAction(userName, accessInt, ip, "admingetServerData失败")
				this.CustomAbort(http.StatusUnauthorized, "权限不足")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}
func (this *WebServerCfgController) escapeC2SString() common.S2WServerData {
	ipC2S := this.Input().Get("ip")
	nameC2S := this.Input().Get("name")
	areaC2S := this.Input().Get("area")
	portC2S := this.Input().Get("port")
	newServerC2S := this.Input().Get("newServer")
	chanIdC2S := this.Input().Get("chan_id")
	/*转换预防XSS*/
	ipEscape := template.JSEscapeString(ipC2S)
	nameEscape := template.JSEscapeString(nameC2S)
	areaEscape := template.JSEscapeString(areaC2S)
	portEscape := template.JSEscapeString(portC2S)
	newServerEscape := template.JSEscapeString(newServerC2S)
	chanIdEscape := template.JSEscapeString(chanIdC2S)
	var syncW2SData common.S2WServerData
	syncW2SData.Ip = ipEscape
	syncW2SData.Name = nameEscape
	syncW2SData.Area = areaEscape
	syncW2SData.Port, _ = strconv.Atoi(portEscape)
	syncW2SData.NewServer, _ = strconv.Atoi(newServerEscape)
	syncW2SData.ChanId = chanIdEscape
	return syncW2SData
}

// "/c2sSave"
func (this *WebServerCfgController) PostSaveServerCfg() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)
		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			{
				escapeData := this.escapeC2SString()
				//保存到数据库
				bsucc := webAdmin.SaveServerCfg2mySql(escapeData)
				if bsucc {
					RecordUserAction(userName, accessInt, ip, "c2sSave保存数据库成功")
					this.CustomAbort(http.StatusOK, "c2sSave保存数据库成功")
				} else {
					RecordUserAction(userName, accessInt, ip, "c2sSave保存数据库失败")
					this.CustomAbort(http.StatusServiceUnavailable, "c2sSave mysql err")
				}
			}
		case common.ACCESS_REPORT:
			fallthrough
		default:
			{
				RecordUserAction(userName, accessInt, ip, "c2sSave失败,权限不足")
				this.CustomAbort(http.StatusUnauthorized, "权限不足")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

// "/c2sDel"
func (this *WebServerCfgController) PostDelServerCfg() {
	if !common.GStopFlag {
		ip := this.Ctx.Input.IP()
		webAdmin := mysql.GetWebAdmin()
		userName, accessInt := webAdmin.GetAccessByIp(ip)

		switch int32(accessInt) {
		case common.ACCESS_ADMIN:
			fallthrough
		case common.ACCESS_SERVER:
			{
				escapeData := this.escapeC2SString()
				//保存到数据库
				bsucc := webAdmin.DelServerCfg2mySql(escapeData)
				if bsucc {
					RecordUserAction(userName, accessInt, ip, "c2sDel删除数据库成功")
					this.CustomAbort(http.StatusOK, "c2sDel删除数据库成功")
				} else {
					RecordUserAction(userName, accessInt, ip, "cc2sDel删除数据库失败")
					this.CustomAbort(http.StatusServiceUnavailable, "c2sDel mysql err")
				}
			}
		case common.ACCESS_REPORT:
			fallthrough
		default:
			{
				RecordUserAction(userName, accessInt, ip, "c2sDel失败,权限不足")
				this.CustomAbort(http.StatusUnauthorized, "权限不足")
			}
		}
	} else {
		this.CustomAbort(http.StatusBadRequest, "服务器关机中")
	}
}

func (this *WebServerCfgController) Finish() {
	requestbody := this.Ctx.Request.Body
	if requestbody != nil {
		defer requestbody.Close()
	}
}
